﻿//-----------------------------------------------------------------------
// <copyright file="MainPage.cs" company="Microsoft Corporation">
//     Copyright Microsoft Corporation. All Rights Reserved. This code released under the terms of the Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.) This is sample code only, do not use in production environments.
// </copyright>
//-----------------------------------------------------------------------
namespace vsarFlightPlan.Utilities
{
    using Windows.Storage;

    /// <summary>
    ///
    /// </summary>
    public static class SettingUtils
    {
        private static ApplicationDataContainer hiddenItems;

        /// <summary>
        /// Hiddens the items container.
        /// </summary>
        /// <returns></returns>
        public static ApplicationDataContainer HiddenItemsContainer()
        {
            if (hiddenItems == null)
            {
                return hiddenItems = ApplicationData.Current.RoamingSettings.CreateContainer("hiddenItems", ApplicationDataCreateDisposition.Always);
            }

            return hiddenItems;
        }

        /// <summary>
        /// Reads the specified container.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="container">The container.</param>
        /// <param name="key">The key.</param>
        /// <param name="default">The default.</param>
        /// <returns></returns>
        public static T Read<T>(ApplicationDataContainer container, string key, T @default)
        {
            if (container.Values.ContainsKey(key))
            {
                return (T)container.Values[key];
            }

            return @default;
        }

        /// <summary>
        /// Settingses the container.
        /// </summary>
        /// <param name="createIfNeeded">if set to <c>true</c> [create if needed].</param>
        /// <returns></returns>
        public static ApplicationDataContainer SettingsContainer(bool createIfNeeded = false)
        {
            if (createIfNeeded)
            {
                return ApplicationData.Current.RoamingSettings.CreateContainer("settings", ApplicationDataCreateDisposition.Always);
            }

            if (ApplicationData.Current.RoamingSettings.Containers.ContainsKey("settings"))
            {
                return ApplicationData.Current.RoamingSettings.Containers["settings"];
            }

            return null;
        }
    }
}